package net.hn.hnms.sys.common.core.utils;

import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

public class HttpUtils {

	public static String post(String url, String body) {
		RestTemplate template = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
		headers.setContentType(type);
		headers.add("Accept", MediaType.APPLICATION_JSON.toString());

		HttpEntity<String> formEntity = new HttpEntity<String>(body, headers);

		String response = template.postForObject(url, formEntity, String.class);

		return response;
	}
    public static String get(String url,HttpHeaders headers){
        RestTemplate template = new RestTemplate();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        headers.putAll(headers);
        HttpEntity<String> requestEntity = new HttpEntity<String>( headers);
       ResponseEntity<String> response= template.exchange(url, HttpMethod.GET, requestEntity, String.class);
        return response.getBody();
    }

    public static String post(String url, String body,HttpHeaders headers) {
        RestTemplate template = new RestTemplate();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        headers.putAll(headers);
        HttpEntity<String> formEntity = new HttpEntity<String>(body, headers);
        String response = template.postForObject(url, formEntity, String.class);

        return response;
    }
}
